#ifndef	_CRT_SOCK_H_
#define	_CRT_SOCK_H_

#include <crt_define.h>

#ifdef	__cplusplus
extern "C" {
#endif

#ifndef NET_PORT
	typedef unsigned short	NET_PORT;
#endif 

#ifndef INVALID_NET_PORT
#define INVALID_NET_PORT 0
#endif 

#ifndef NET_ADDR
	typedef const char*		NET_ADDR;
#endif 

#ifndef NET_ADDR_INT
	typedef unsigned int	NET_ADDR_INT;
#endif 


#ifndef BUFFER_PTR
	typedef unsigned char*	BUFFER_PTR;
#endif

#ifndef BUFFER_SIZE
	typedef unsigned long	BUFFER_SIZE;
#endif


	/*
	Htons converts unsigned short types from host order to network order
	Htonl converts unsigned long type from host order to network order
	NTOHS converts unsigned short types from network order to host order
	NTOHL converts unsigned long types from network order to host order
	*/


#if (TARGET_OS == OS_WINDOWS)
	typedef SOCKET              _sock_t;

	#ifndef socklen_t
	typedef int				    socklen_t;
	#endif

	#define SHUT_SOCK_RD			SD_RECEIVE
	#define SHUT_SOCK_RW			SD_SEND
	#define SHUT_SOCK_BOTN			SD_BOTH

#elif (TARGET_OS == OS_POSIX)

	#define INVALID_SOCKET      -1
	typedef int                 _sock_t;
	#define SHUT_SOCK_RD		SHUT_RD
	#define SHUT_SOCK_RW		SHUT_WR
	#define SHUT_SOCK_BOTN		SHUT_RDWR

#elif (TARGET_OS == OS_DARWIN)

	#define INVALID_SOCKET      -1
	typedef int                 _sock_t;
	#define SHUT_SOCK_RD		SHUT_RD
	#define SHUT_SOCK_RW		SHUT_WR
	#define SHUT_SOCK_BOTN		SHUT_RDWR

#endif

_sock_t _createsock(
	int af,
	int type,
	int protocol);

int _closesock(_sock_t s);

int _shutdown_sock(_sock_t s,int howrw);

int SetError(_sock_t* s, int turn);
int SetReuseAddr(_sock_t* s, int turn);
int SetNoDelay(_sock_t* s, int turn);
int SetBroadCast(_sock_t* s, int turn);
int SetDontRoute(_sock_t* s, int turn);
int SetKeepalive(_sock_t* s, int turn);
int SetCork(_sock_t* s, int turn);
int SetNoSigPipe(_sock_t* s, int turn);
int SetNonblock(_sock_t* s, int turn);
int SetDoLinger(_sock_t* s, int turn);
int SetLinger(_sock_t* s, int sec);
int SetSendTimeOut(_sock_t* s, int sec);
int SetRecvTimeOut(_sock_t* s, int sec);
int SetConTimeOut(_sock_t* s, int sec);
int SetSendBufLen(_sock_t* s, int size);
int SetRecvBufLen(_sock_t* s, int size);
int SetSendLoWat(_sock_t* s, int size);
int SetRecvLoWat(_sock_t* s, int size);

#if (TARGET_OS == OS_WINDOWS)
	int socketpair(int d, int type, int protocol, _sock_t sv[2]);
#endif

#ifdef	__cplusplus
}
#endif

#endif


